﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace Labirinto_Magico
{
        public struct TutorialSound
    {
        public SoundEffect Part_1;
        public SoundEffect Part_2;
        public SoundEffect Part_3;
        public SoundEffect Part_4;
        public SoundEffect Part_5;
    }

    public class SoundHandler
    {
        SoundEffect mGoalCatch  = null;
        SoundEffect mSlide      = null;
        SoundEffect mVictory    = null;
        SoundEffect mButton     = null;
        SoundEffect mStartGame  = null;
        public Song mTheme = null;
        SoundEffect mBouncing    = null;
        SoundEffect mError = null;

        public TutorialSound mTutorial;
        List<SoundEffectInstance> mTutorialParts;

        TimeSpan mTime;

        public void Init(ContentManager content)
        {
            mTheme = content.Load<Song>("Audio/theme");

            MediaPlayer.IsRepeating = true;
            MediaPlayer.Volume = 0.5f;
            MediaPlayer.Play(mTheme);

            mGoalCatch = content.Load<SoundEffect>("Audio/goalCatched");
            mSlide = content.Load<SoundEffect>("Audio/slide");
            mVictory = content.Load<SoundEffect>("Audio/victory2");
            mStartGame = content.Load<SoundEffect>("Audio/start");
            mButton = content.Load<SoundEffect>("Audio/button");
            mBouncing = content.Load<SoundEffect>("Audio/bouncing");
            mError = content.Load<SoundEffect>("Audio/chord");

            InitTutorial(content);

            mTime = new TimeSpan(0,0,0);
            mTutorialParts = new List<SoundEffectInstance>();
        }

        public void GoalSound()
        {
            mGoalCatch.Play();
        }

        public void SliderSound()
        {
            mSlide.Play();
        }

        public void VictorySound()
        {
            MediaPlayer.Pause();
            mVictory.Play(0.3f, 0, 0);
        }

        public void ButtonSound()
        {
            mButton.Play(0.3f, 0, 0);
        }

        public void StartSound()
        {
            mStartGame.Play();
        }

        public void BounceSound()
        {
            mBouncing.Play();
        }

        public void ErrorSound()
        {
            mError.Play();
        }

        public bool PlayTutorial(SoundEffect iTutorialPart, TimeSpan iTime)
        {
            SoundEffectInstance instance = null;
            if (mTime == new TimeSpan(0, 0, 0))
            {
                mTime = iTime;
                MediaPlayer.Pause();
                instance = iTutorialPart.CreateInstance();
                mTutorialParts.Add(instance);
                instance.Play();
            }

            double temp = iTutorialPart.Duration.TotalMilliseconds - iTime.Subtract(mTime).TotalMilliseconds;

            if (temp < 0)
            {
                mTime = new TimeSpan(0, 0, 0);
                instance = mTutorialParts.ElementAt(0);
                mTutorialParts.Remove(instance);
                MediaPlayer.Resume();
                return true;
            }
            return false;
        }

        void InitTutorial(ContentManager content)
        {
            mTutorial.Part_1 = content.Load<SoundEffect>("Audio/Tutorial_1");
            mTutorial.Part_2 = content.Load<SoundEffect>("Audio/Tutorial_2");
            mTutorial.Part_3 = content.Load<SoundEffect>("Audio/Tutorial_3");
            mTutorial.Part_4 = content.Load<SoundEffect>("Audio/Tutorial_4");
            mTutorial.Part_5 = content.Load<SoundEffect>("Audio/Tutorial_5");
        }

        public void StopTutorial()
        {
            foreach (SoundEffectInstance i in mTutorialParts)
                i.Stop();
            mTutorialParts.Clear();
            MediaPlayer.Resume();
            mTime = new TimeSpan(0, 0, 0);
        }
    }
}
